package com.education.service.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.education.result.R;
import com.education.service.edu.entity.Course;
import com.education.service.edu.entity.Teacher;
import com.education.service.edu.entity.query.TeacherQuery;
import com.education.service.edu.feign.OssFileService;
import com.education.service.edu.mapper.CourseMapper;
import com.education.service.edu.mapper.TeacherMapper;
import com.education.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author Lance
 * @since 2020-06-16
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Autowired
    private OssFileService ossFileService;

    @Autowired
    private CourseMapper courseMapper;

    /**
     * 分页条件查询
     * @param page
     * @param limit
     * @param teacherQuery  查询对象
     */
    @Override
    public Page<Teacher> selectPage(Long page, Long limit, TeacherQuery teacherQuery) {
        // 分页对象
        Page<Teacher> pageParam = new Page<>(page, limit);
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        // 在查询之前进行排序
        queryWrapper.orderByAsc("sort");
        // 根据 teacherQuery 进行查询
        if (teacherQuery == null){
            return baseMapper.selectPage(pageParam, queryWrapper);
        }
        // 进行组装查询条件
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getJoinDateBegin();
        String end = teacherQuery.getJoinDateEnd();

        if (!StringUtils.isBlank(name)) {
            // likeRight 解决并发访问量大
            queryWrapper.likeRight("name", name);
        }

        // 级别
        if (level != null) {
            queryWrapper.eq("level", level);
        }

        // 入住时间
        if (!StringUtils.isBlank(begin)) {
            queryWrapper.ge("join_date", begin);
        }

        // 结束时按
        if (!StringUtils.isBlank(end)) {
            queryWrapper.le("join_date", end);
        }

        return baseMapper.selectPage(pageParam, queryWrapper);
    }

    /**
     * 删除讲师头像
     * @param id
     * @return
     */
    @Override
    public boolean removeAvatarById(String id) {
        Teacher teacher = baseMapper.selectById(id);
        // 判断讲师是否有头像
        if(teacher != null) {
            String avatar = teacher.getAvatar();
            if(!StringUtils.isEmpty(avatar)){
                //删除图片
                R r = ossFileService.removeFile(avatar);
                return r.getSuccess();
            }
        }
        return false;
    }

    /**
     * 根据关键字实现快速查询讲师信息
     * @param key
     * @return
     */
    public List<Map<String, Object>> selectNameListByKey(String key) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("name");
        queryWrapper.likeRight("name", key);
        // 返回值是Map列表
        List<Map<String, Object>> list = baseMapper.selectMaps(queryWrapper);
        return list;
    }

    /**
     * 根据讲师id获取讲师详情页数据
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> selectTeacherInfoById(String id) {
        //获取讲师信息
        Teacher teacher = baseMapper.selectById(id);
        //根据讲师id获取讲师课程
        List<Course> courseList =  courseMapper.selectList(new QueryWrapper<Course>().eq("teacher_id", id));

        Map<String, Object> map = new HashMap<>();
        map.put("teacher", teacher);
        map.put("courseList", courseList);
        return map;
    }

    /**
     * 获取首页推荐前4条讲师数据 [ 使用redis缓存 ]
     * @return
     */
    @Cacheable(value = "index", key = "'selectHotTeacher'")
    @Override
    public List<Teacher> selectHotTeacher() {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        queryWrapper.last("limit 4");
        return baseMapper.selectList(queryWrapper);
    }
}
